import sys
import warnings

from PyQt6.QtCore import QSize, Qt
from PyQt6.QtGui import QPainter, QPixmap, QIcon
from PyQt6.QtWidgets import QApplication, QWidget, QToolButton, QMessageBox
from PyQt6 import uic

from front.bridge import BridgeWindow
from front.mulWave import MulWaveWindow
from front.jointWellNum import JointWellNumWindow
from front.inclinedShaft import InclinedShaftWindow
from utils.error import getError
from utils.index import resource_path


class MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(resource_path('static/ui/primary.ui'), self)  # 加载 UI 文件
        self.init_style()
        self.init_event()

    # ==============================
    # 方法重写
    # ==============================

    def paintEvent(self, event):
        # 在 paintEvent 中绘制背景图片
        painter = QPainter(self)
        pixmap = QPixmap(resource_path("static/image/primary_bg.png"))
        painter.drawPixmap(self.rect(), pixmap)  # 使用 pixmap 填充整个窗口

    def resizeEvent(self, event):
        # 根据窗口宽度动态调整
        width = self.width()  # 获取窗口当前宽度
        # 根据窗口宽度动态调整图标大小
        buttons = self.findChildren(QToolButton)
        for btn in buttons:
            icon_size = int(width / 10)
            btn.setIconSize(QSize(icon_size, icon_size))

        super().resizeEvent(event)

    # ==============================
    # 初始化
    # ==============================

    # 初始化样式
    def init_style(self):
        # 设置窗口图标和标题
        self.ui.setWindowTitle("SeismicPlot")
        self.ui.setWindowIcon(QIcon(resource_path('static/image/favicon.ico')))
        self.ui.toolButton.setIcon(QIcon(resource_path('static/image/primary_btn1.png')))
        self.ui.toolButton_2.setIcon(QIcon(resource_path('static/image/primary_btn2.png')))
        self.ui.toolButton_3.setIcon(QIcon(resource_path('static/image/primary_btn3.png')))
        self.ui.toolButton_4.setIcon(QIcon(resource_path('static/image/primary_btn4.png')))

        for widget in self.findChildren(QToolButton):
            widget.setStyleSheet("background-color: white;")

    # 设置按钮点击事件
    def init_event(self):
        self.ui.toolButton.clicked.connect(self.open_bridge_window)  # 打开桥图窗口
        self.ui.toolButton_2.clicked.connect(self.open_inclined_shaft_window)  # 打开桥图（斜井）窗口
        self.ui.toolButton_3.clicked.connect(self.open_mul_wave_window)  # 打开多次波窗口
        self.ui.toolButton_4.clicked.connect(self.open_joint_well_num_window)  # 打开联井数窗口

    # 桥图窗口
    def open_bridge_window(self):
        self.bridge_window = BridgeWindow()  # 将子窗口保存为类属性
        self.bridge_window.show()

    # 桥图（斜井）
    def open_inclined_shaft_window(self):
        self.inclined_shaft_window = InclinedShaftWindow()  # 将子窗口保存为类属性
        self.inclined_shaft_window.show()

    # 多次波窗口
    def open_mul_wave_window(self):
        self.mul_wave_window = MulWaveWindow()  # 将子窗口保存为类属性
        self.mul_wave_window.show()

    # 联井数
    def open_joint_well_num_window(self):
        self.joint_well_num_window = JointWellNumWindow()
        self.joint_well_num_window.show()


if __name__ == "__main__":
    # 解决警告“DeprecationWarning: sipPyTypeDict() is deprecated”
    warnings.filterwarnings("ignore", category=DeprecationWarning)
    # 解决图片显示不清晰的问题
    QApplication.setHighDpiScaleFactorRoundingPolicy(Qt.HighDpiScaleFactorRoundingPolicy.PassThrough)

    try:
        app = QApplication(sys.argv)
        window = MainWindow()
        window.show()  # 显示主窗口
        sys.exit(app.exec())  # 进入主循环
    except Exception as e:
        # 捕获异常并显示错误信息
        err = getError()
        QMessageBox.critical(None, f"{err['type']}",
                                    f"{err['msg']}, file in {err['file']}, line in {err['line']}.")
        # sys.exit(1)  # 退出程序，返回错误代码 1